package com.example.lzy.listviewsample.GridView;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TabHost;
import android.widget.Toast;

import com.example.lzy.listviewsample.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class GridViewActivity extends AppCompatActivity {

    private GridView gridView = null;
    //图标
    private  int[] iconArray = {R.drawable.two_icon,R.drawable.two_icon,R.drawable.two_icon,
            R.drawable.two_icon,R.drawable.two_icon,R.drawable.two_icon,
            R.drawable.two_icon,R.drawable.two_icon,R.drawable.two_icon};


    //应用名称
    private String[] appName = {"1","2","3","4","5","6","7","8","9"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.gridview_layout);

        gridView = (GridView) findViewById(R.id.gridView);

        List<Map<String,Object>> list = new ArrayList<>();

        for (int i = 0;i<iconArray.length;i++){
            Map<String,Object> map = new HashMap<>();
            map.put("icon",iconArray[i]);
            map.put("name",appName[i]);
            list.add(map);
        }

        SimpleAdapter adapter = new SimpleAdapter(this,list,R.layout.gridview_item,new String[] {"icon","name"},new int[]{R.id.img,R.id.name_tv});

        gridView.setAdapter(adapter);

        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Toast.makeText(GridViewActivity.this, appName[i], Toast.LENGTH_SHORT).show();
            }
        });
    }
}
